package com.cl.cloud.tenant.logDemo;

import lombok.Data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description:
 * @author: liuzijian
 * @time: 2023-01-12 14:16
 */
public class Test {

    public static void main(String[] args) {
        Integer maxAIndex = 0;
        Integer maxBIndex = 0;
        File file = new File("C:\\Users\\ADMIN\\Desktop\\印章测试");
        if (file.isDirectory()) {
            File[] fileArray = file.listFiles();
            for (int i = 0; i < fileArray.length; i++) {
                File itemFile = fileArray[i];
                if (itemFile.getName().startsWith("A")) {
                    maxAIndex = Math.max(maxAIndex, Integer.parseInt(itemFile.getName().split("-")[1].replaceAll(".txt", "")));
                } else if (itemFile.getName().startsWith("B")) {
                    maxBIndex = Math.max(maxBIndex, Integer.parseInt(itemFile.getName().split("-")[1].replaceAll(".txt", "")));
                }
            }
        }
        Integer 线程数 = 5;
        Integer minIndex = -1;
        Integer maxIndex = -1;
        if (maxAIndex > maxBIndex) {
            minIndex = maxBIndex;
            maxIndex = maxAIndex;
        } else if (maxBIndex > maxAIndex) {
            minIndex = maxAIndex;
            maxIndex = maxBIndex;
        } else {
            minIndex = maxIndex = maxBIndex;
        }
        List<LogFile> logFiles = new LinkedList<>();
        for (int i = 1000; i < minIndex; i += 1000) {
            LogFile logFile = new LogFile();
            logFile.setAIndex(i);
            logFile.setBIndex(i);
            logFiles.add(logFile);
        }
        for (int i = minIndex; i <= maxIndex; i += 1000) {
            LogFile logFile = new LogFile();
            if (maxIndex.equals(maxAIndex)) {
                logFile.setAIndex(i);
            } else if (maxIndex.equals(maxBIndex)) {
                logFile.setBIndex(i);
            }
            logFiles.add(logFile);
        }
        ThreadPoolExecutor executor = new ThreadPoolExecutor(线程数, 线程数, 0, TimeUnit.SECONDS, new LinkedBlockingDeque<>(), new ThreadFactory() {
            private final AtomicInteger num = new AtomicInteger(1);

            @Override
            public Thread newThread(Runnable runnable) {
                return new Thread(runnable, "读取log线程-" + num.getAndAdd(1));
            }
        });
        for (LogFile logFile : logFiles) {
            executor.submit(logFile::doRead);
        }
    }

    @Data
    public static class LogFile {
        private Integer aIndex = -1;
        private Integer bIndex = -1;

        public void doRead() {
            File aLog = new File("C:\\Users\\ADMIN\\Desktop\\印章测试" + "\\A-" + aIndex + ".txt");
            File bLog = new File("C:\\Users\\ADMIN\\Desktop\\印章测试" + "\\B-" + aIndex + ".txt");
            if (aLog.exists() && bLog.exists()) {
                try (BufferedReader readerA = new BufferedReader(new FileReader(aLog)); BufferedReader readerB = new BufferedReader(new FileReader(bLog))) {
                    String readLineA;
                    String readLineB;
                    while ((readLineA = readerA.readLine()) != null && (readLineB = readerB.readLine()) != null) {
                        System.out.println(readLineA);
                        System.out.println(readLineB);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (aLog.exists()) {
                try (BufferedReader readerA = new BufferedReader(new FileReader(aLog));) {
                    String readLineA;
                    while ((readLineA = readerA.readLine()) != null) {
                        System.out.println(readLineA);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (bLog.exists()) {
                try (BufferedReader readerB = new BufferedReader(new FileReader(bLog))) {
                    String readLineB;
                    while ((readLineB = readerB.readLine()) != null) {
                        System.out.println(readLineB);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
